package com.zjs.controller;


import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import com.zjs.common.bean.PageQuery;
import com.zjs.common.bean.ResponseResult;
import com.zjs.common.utils.UUID32Gen;
import com.zjs.pojo.BcStaff;
import com.zjs.service.Impl.BcDecidedZoneImpl;
import com.zjs.service.Impl.BcStaffServiceImpl;
import com.zjs.vo.BcDecidedZoneVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "bcStaff")
public class BcStaffController {




    @Autowired
    BcStaffServiceImpl  bcStaffService;

    @Autowired
    BcDecidedZoneImpl bcDecidedZone;

    //添加bcStaff操作

    @RequestMapping(value = "saveOrUp.action")
    @ResponseBody
    public ResponseResult saveOrUp(BcStaff bcStaff){


        //添加操作
        int i = bcStaffService.saveStaffOrUp(bcStaff);


        if (i>0){
            return  new ResponseResult(true,null);
        }else{
            return new ResponseResult(false,null);
        }
    }


    //分页查看出派送员的信息 用到了mybatis中的分页插件
    @RequestMapping(value = "/findPage.action",method = {RequestMethod.POST})
    //注释的权限控制
    @RequiresRoles("chaojiguanliyuan")  //要求的角色
    @RequiresPermissions("staff")  //要求的权限
    @ResponseBody
   public Map<String,Object> findAllBcStaff(PageQuery query) throws UnsupportedEncodingException {


        String keyword = query.getKeyword();

        if (!StringUtils.isNullOrEmpty(keyword)){
            query.setKeyword("%"+new String(keyword.getBytes("ISO8859-1"),"utf-8")+"%");
        }

        PageInfo<BcStaff> bcStaffByLimit = bcStaffService.findBcStaffByLimit(query);

        Map<String,Object> map=new HashMap<>();
        map.put("total",bcStaffByLimit.getTotal());   //存储总记录数
        map.put("rows",bcStaffByLimit.getList());

        return map;

   }

   //批量删除派送员的信息  项目中最好不删除  而是改变其状态为1
    @RequestMapping(value = "delBcStaff.action")
    @ResponseBody  //返回的是一个json对象
    public  ResponseResult   delBcStaff(String ids){


        int i = bcStaffService.delBcStaff(ids);

        if(i>0){
            return new ResponseResult(true,null);
        }else{
            return new ResponseResult(false,null);
        }

    }


    //还原操作
    @RequestMapping(value = "uPBcStaffDelTag.action")
    @ResponseBody
    public ResponseResult uPBcStaffDelTag(String ids){

        int i = bcStaffService.upBcStaffDelTag(ids);


        if (i>0){
            return new ResponseResult(true,null);

        }else{

            return new ResponseResult(false,null);
        }
    }


    //修改操作第一步 通过id得到显示的数据

    @RequestMapping(value = "uPBcStaffById.action")
    @ResponseBody  //返回的是json格式的字符串
    public BcStaff uPBcStaffByIdOne(@RequestParam String id){

        BcStaff bcStaffById = bcStaffService.findBcStaffById(id);

        return bcStaffById;
    }



//    findListWithoutDel.action

    @RequestMapping(value = "findListWithoutDel.action")
    @ResponseBody
     public  List<BcStaff>  findListWithoutDel(String id){

        List<BcStaff> listWithoutDel = bcStaffService.findListWithoutDel();

        List<BcStaff> bcStaffs=new ArrayList<>();

        for (BcStaff bcStaff:listWithoutDel){

            boolean bindDecided = bcDecidedZone.isBindDecided(bcStaff.getId());

            //如果不为true 则代表没有定区绑定该 快递员
            if (!bindDecided){

                bcStaffs.add(bcStaff);

            }
        }

        if (!StringUtils.isNullOrEmpty(id)){
            //通过decided_id  得到 staff_id

            BcDecidedZoneVo decidedById = bcDecidedZone.findDecidedById(id);

            BcStaff staff = decidedById.getStaff();
            bcStaffs.add(staff);
        }

        return bcStaffs;
     }




}
